using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using XQ.RequestOfficeSupplies.BLL.Business;
using XQ.RequestOfficeSupplies.DAL.DAL.Common;
using XQ.RequestOfficeSupplies.DAL.Model.Business;
using XQ.RequestOfficeSupplies.DAL.Model;
using XQ.RequestOfficeSupplies.DAL.Models;
using XQ.RequestOfficeSupplies.DAL;
using XQ.RequestOfficeSupplies.WebAPI.Common;
using XQ.RequestOfficeSupplies.WebAPI.Models;

namespace XQ.RequestOfficeSupplies.WebAPI.Controllers.Business
{
    /// <summary>
    /// 库存表接口
    /// </summary>
    public class InventoryController : ApiController
    {
        InventoryBLL inventoryBLL = new InventoryBLL();

        /// <summary>
        /// 获取库存表列表
        /// </summary>
        /// <param name="ssm"></param>
        /// <returns></returns>
        [Route("api/Inventory/GetList")]
        [HttpGet]
        public ResultModel GetList([FromUri] InventorySearchModel ssm)
        {
            ResultModel resultModel = new ResultModel();
            Pagination page = new Pagination();
            ssm = ssm == null ? new InventorySearchModel() : ssm;
            try
            {
                TokenModel token = TokenHelper.GetTokenByHeaders(Request.Headers);
                resultModel.data = inventoryBLL.GetList(ssm, page, token);
                resultModel.page = page;
                LogBll.Instance.WriteLog(LogBll.LogType.Search, "", token.UID, "获取库存表列表", "", "", "", "api/Inventory/GetList：" + JsonConvert.SerializeObject(ssm) + "|" + JsonConvert.SerializeObject(token));
            }
            catch (Exception ex)
            {
                resultModel.success = false;
                resultModel.message = ex.Message;
                resultModel.code = "10001";
            }

            return resultModel;
        }

        /// <summary>
        /// 保存库存表信息（新增和编辑）
        /// </summary>
        /// <returns></returns>
        [Route("api/Inventory/SaveInventory")]
        [HttpPost]
        public ResultModel SaveInventory([FromBody] Inventory inventory)
        {
            ResultModel resultModel = new ResultModel();
            try
            {
                TokenModel token = TokenHelper.GetTokenByHeaders(Request.Headers);
                resultModel.data = inventoryBLL.SaveInventory(inventory, token);
                LogBll.Instance.WriteLog(LogBll.LogType.AddNew, "", 0, "保存库存表信息（新增和编辑）", "", "", "", "api/Inventory/SaveInventory：" + JsonConvert.SerializeObject(inventory));
            }
            catch (Exception ex)
            {
                LogBll.Instance.WriteErrLog("", 0, "保存库存表信息（新增和编辑）报错", "", "", JsonConvert.SerializeObject(inventory), ex.Message, "", "InventoryController", "SaveInventory");
                resultModel.success = false;
                resultModel.message = ex.Message;
                resultModel.code = "10001";
            }

            return resultModel;
        }



        /// <summary>
        /// 获取库存表信息详情
        /// </summary>
        /// <param name="id">InventoryID</param>
        /// <returns></returns>
        [Route("api/Inventory/GetInventoryByID")]
        [HttpGet]
        public ResultModel GetInventoryByID(Guid id)
        {
            ResultModel resultModel = new ResultModel();
            try
            {
                resultModel.data = inventoryBLL.GetInventoryByID(id);
            }
            catch (Exception ex)
            {
                resultModel.success = false;
                resultModel.message = ex.Message;
                resultModel.code = "10001";
            }

            return resultModel;
        }

        /// <summary>
        /// 批量删除{tableCnName}信息·························
        /// </summary>
        /// <param name="ids"></param>
        [Route("api/Inventory/DeleteByIds")]
        [HttpPost]
        public ResultModel DeleteByIds(List<Guid> ids)
        {
            ResultModel resultModel = new ResultModel();
            try
            {
                TokenModel token = TokenHelper.GetTokenByHeaders(Request.Headers);
                resultModel.data = inventoryBLL.DeleteByIds(ids);
                LogBll.Instance.WriteLog(LogBll.LogType.Delete, "", token.UID, "批量删除库存表信息", "", "", "", "api/Inventory/DeleteByIds：" + JsonConvert.SerializeObject(ids));
            }
            catch (Exception ex)
            {
                resultModel.success = false;
                resultModel.message = ex.Message;
                resultModel.code = "10001";
            }

            return resultModel;
        }
    }
}